package site.marqstree.latte.ec.main.cart;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DiffUtil;

import site.marqstree.latte.ui.recycler.MultipleFields;
import site.marqstree.latte.ui.recycler.MultipleItemEntity;

/*
 * 项目名: EleCommerce
 * 包名: site.marqstree.latte.ec.main.cart
 * 文件名: ShopCartItemDiffCallback
 * 创建者: marqstree
 * 创建时间: 2020/2/2 13:05
 * 描述: setDiffNewData函数加载新数据时才触发
 * 修改适配器原有数据，不会触发
 */
public class ShopCartItemDiffCallback extends DiffUtil.ItemCallback<MultipleItemEntity> {


    @Override
    public boolean areItemsTheSame(@NonNull MultipleItemEntity oldItem, @NonNull MultipleItemEntity newItem) {
        return oldItem.getField(MultipleFields.ID) == newItem.getField(MultipleFields.ID);
    }

    @Override
    public boolean areContentsTheSame(@NonNull MultipleItemEntity oldItem, @NonNull MultipleItemEntity newItem) {
        return ((String)oldItem.getField(MultipleFields.IMAGE_URL)).equals((String)newItem.getField(MultipleFields.IMAGE_URL))
                && ((String)oldItem.getField(ShopCartItemFields.TITLE)).equals((String)newItem.getField(ShopCartItemFields.TITLE))
                && ((String)oldItem.getField(ShopCartItemFields.DESC)).equals((String)newItem.getField(ShopCartItemFields.DESC))
                && ((Integer)oldItem.getField(ShopCartItemFields.COUNT)).equals((Integer)newItem.getField(ShopCartItemFields.COUNT))
                && ((Double)oldItem.getField(ShopCartItemFields.PRICE)).equals((Double)newItem.getField(ShopCartItemFields.PRICE))
                && ((Boolean)oldItem.getField(ShopCartItemFields.IS_SELECTED)).equals((Boolean)newItem.getField(ShopCartItemFields.IS_SELECTED));
    }

    @Nullable
    @Override
    public Object getChangePayload(@NonNull MultipleItemEntity oldItem, @NonNull MultipleItemEntity newItem) {
        if(!((Boolean)oldItem.getField(ShopCartItemFields.IS_SELECTED)).equals((Boolean)newItem.getField(ShopCartItemFields.IS_SELECTED)))
            return ShopCartAdapter.ITEM_SELECTED_PAYLOAD;
        else
            return super.getChangePayload(oldItem, newItem);
    }
}
